'use strict';
// Copyright 2014 Blueant Inc. All Rights Reserved.

/**
 * @fileoverview 加入公会
 * @author sundbcn@gmail.com (sun debing)
 */

var async = require('async');
var moment = require('moment');
var constantUtil = require('../../../../util/constantUtil');
var Code = require('../../../../consts/code');
var unionGroupModel = require('../../../../models/unionGroupModel');
var unionGroupConf = require('../../../../config/unionGroupConf');
var PlayerManager = require('../../../../libs/playerManager');

/**
 * 加入公会
 */
module.exports = function (msg, session, next) {
    var role_id = session.uid;
    var union_id = msg.union_id;

    if (!union_id) {
        return next(null, {
            code: Code.PARAM_ERROR
        });
    }

    var union_member_num;
    var union_group_model;
    var role_model;
    var player_manager = new PlayerManager(role_id);
    async.waterfall(
        [
            function (cb) {
                player_manager.refreshData(cb);
            },
            function (cb) {
                player_manager.getModel(['role', 'accrued'], cb);
            },
            function (cb) {
                if (!!union_id) {
                    unionGroupModel.getById(union_id, function (err, model) {
                        union_group_model = model;
                        cb();
                    });
                } else {
                    return next(null, {
                        code: Code.UNION_IS_NOT_JOIN
                    });
                }
            },
            function (cb) {
                role_model = player_manager.role;

                // 是否已经加入公会
                if (!!role_model.getUnionId()) {
                    return next(null, {
                        code: Code.UNION_IS_JOIN
                    });
                }

                // 当天是否已申请过该公会
                if (!union_group_model.isJoinOnlyApplyList(role_id)) {
                    return next(null, {
                        code: Code.UNION_HAVE_APPLY_NOW_DAY
                    });
                }

                // 限制加入等級
                if (role_model.getLv() < union_group_model.limit_level) {
                    return next(null, {
                        code: Code.UNION_APPLY_ROLE_LEVEL_NOT_ENOUGH
                    });
                }

                var member_limit_num = unionGroupConf.getUnionMemberLimitNum(union_group_model.getUnionLv());
                union_member_num = union_group_model.union_user_list.length;

                // 公会人数是否达到上限
                if (union_member_num >= member_limit_num) {
                    return next(null, {
                        code: Code.UNION_MEMBERS_REACH_LIMIT
                    });
                }

                // //申请的公会人数是否达到上限
                //var union_user_num = union_group_model.union_user_list.length;
                //if (union_user_num >= member_limit_num) {
                //return next(null, {
                //code: Code.UNION_MEMBERS_REACH_LIMIT
                //});
                //}

                // 增加公会申请记录
                union_group_model.addJoinOnlyApplyList(role_id);
                union_group_model.addApply(role_id);
                cb();
            },
            function (cb) {
                player_manager.save(cb);
            }
        ],
        function (err) {
            if (!!err) {
                console.error(err);
                return next(null, {
                    code: Code.FAIL
                });
            }
            union_group_model.save();
            return next(null, {
                code: Code.OK
            });
        }
    );
};
